<?php

use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\grid\GridView;
use yii\bootstrap\Modal;
use yii\helpers\Url;
use yii\widgets\Pjax;
use app\common\Functions;
/**
 * @var yii\web\View $this
 * @var amnah\yii2\user\Module $module
 * @var array $actions
 */
global $regionTypes2;
global $prodAttrCodes;
$regionTypes2 = $regionTypes;
$module = $this->context->module;

$this->title = Yii::t('user', 'Yii 2 User');
?>



    <p>
        <?php
            $createUrl                     = [];
            $createUrl[]                   = 'create';
            $createUrl['prod_spec_id']   = $prod_spec_id;
            $createUrl['prod_spec_name'] = $prodSpecName;
            $createUrl['last_url']         = $now_url;

            if ($parent_prod_id)
            {
                $createUrl['parent_prod_id'] = $parent_prod_id;
            }

            $createUrlModel                     = [];
            $createUrlModel['id']            =  'activity-create-prod-link';
            $createUrlModel['class']        = 'btn btn-sm  btn-success activity-create-prod-link';
            $createUrlModel['data-toggle'] = 'modal';
            $createUrlModel['data-target'] = '#create-prod-modal';

            $createUrlModel['label'] = '创建新产品';
            $createUrlModel['title'] = '创建新产品';


            $createUrlModel ['data-prod_spec_id']   = $prod_spec_id;
            $createUrlModel ['data-prod_spec_name'] = $prodSpecName;
            $createUrlModel ['data-last_url']         = $now_url;


        $created                     = [];
        $created['id']            =  'created-prod-link';
        $created['class']        = 'btn btn-sm  btn-success created-prod-link';
        $created['data-toggle'] = 'modal';
        $created['data-target'] = '#created-prod-modal';

        $created['label'] = '创建新产品';
        $created['title'] = '创建新产品';

        $created ['data-prod_spec_id']   = $prod_spec_id;
        $created ['data-prod_spec_name'] = $prodSpecName;
        $created ['data-last_url']         = $now_url;


            if ($parent_prod_id)
            {
                $createUrlModel['data-parent_prod_id'] = $parent_prod_id;
            }
            //创建区域逻辑
            // 如果创建房源
            echo Html::a( Html::tag('span', Yii::t('region', 'Create') .  $prodSpecName , ['class' => "glyphicon fa fa-plus-square"])   . $region_name. Yii::t('region', $region_type_name) , '#', $createUrlModel);

            echo '&nbsp&nbsp';
            //业务2部创建房源
            echo Html::a( Html::tag('span', '业务2部创建房源' .  $prodSpecName , ['class' => "glyphicon fa fa-plus-square"])   . $region_name. Yii::t('region', $region_type_name) , '#', $created);
        ?>

        <?php
        $check =strpos($url, '?');
        if($check !== false) {
            $url = strstr($url, '&a=');
            $url = substr($url, 24);
            echo Html::a(Yii::t('region', 'Back'), $url, ['class' => 'btn btn-default pull-right']);
        }

        ?>
    </p>


    <?php if ($yw2!=2): ?><!--判断是否从楼房进来的  是就显示 不是就隐藏-->
    <div class="box box-default color-palette-box"><!--列表顶部的按钮-->
        <div class="box-body">

        <?php foreach (array_reverse($allFloors) as $floor): ?>
            <div class="row">
                <div class="col-sm-2 col-md-1 tb-floor">
                    <div class="col-md-4 tb-floor">
                        <h2 style="font-size: 18px;"><?php echo $floor['floor'] . 'F';  ?> </h2>
                    </div>
                    <div class="col-md-8 tb-floor" style="font-size: 12px;">
                         <?php echo $floor['totalArea'] . '㎡';  ?>
                    </div>
                </div>
                <div class="col-sm-10 col-md-11 tb-floor">
                <?php foreach ($floor['rooms'] as $room): ?>
                <div class="col-sm-<?= $room['length'] ?> col-md-<?= $room['length'] ?> tb-floor tb-one-room" >
                    <div class="color-palette-set">
                        <?php if ($room['state']==1001): ?>

                            <?php
                            echo Html::a($room['room']. '室 '.$room['area'] .'㎡', // ['view', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,    'prod_spec_name' => $prodSpecName]
                        '#' ,
                        ['id' => 'activity-view-link',
                        'class' => "btn btn-block btn-xs bg-green activity-view-link color-palette",
                        'data-toggle'=>'modal',
                        'data-target'=>'#view-modal',

                        'data-prod-id'=>$room['id'],
                        'data-prod-spec-id'=> $room['prod_spec_id'],
                        'data-prod-spec-name' => $prodSpecName]);
                            ?>

                        <?php else: ?>
                            <?php
                            echo Html::a($room['room']. '室 '.$room['area'] .'㎡'  . $room['cust_name'], // ['view', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,    'prod_spec_name' => $prodSpecName]
                                '#' ,
                                ['id' => 'activity-view-link',
                                    'class' => "btn btn-block btn-xs bg-gray activity-view-link color-palette",
                                    'data-toggle'=>'modal',
                                    'data-target'=>'#view-modal',

                                    'data-prod-id'=>$room['id'],
                                    'data-prod-spec-id'=> $room['prod_spec_id'],
                                    'data-prod-spec-name' => $prodSpecName]);
                            ?>


                        <?php endif; ?>


                    </div>
                </div>
            <?php endforeach; ?>
                </div>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

    <?php

    // 显示各个楼层





        $prodAttrCodes=[];

       // Yii::trace("=====================              ============");
        //Yii::trace($dataProvider);


        // 增加展示的字段
        $columns = [];

        $i = 'uid';
        // 复选框
        $columns[$i]['class'] = 'yii\grid\CheckboxColumn';
        $columns[$i]['multiple'] = true;
        $columns[$i]['name'] = 'uid';
        $columns[$i]['options'] = ['style'=>'width:30px'];


        $i = 'use_cust_id';
        // 承租方
        $columns[$i]['label'] = '承租方';
        $columns[$i]['attribute'] = 'use_cust_id';
        $columns[$i]['value']   = function($model) use ($domainTypes)
        {
            return $model->useCusts->cust_name;

        };







        // id
        $i='id';
        $columns[$i]['attribute'] = 'id';
        $columns[$i]['options'] = ['style'=>'width:30px'];


        //名称
        $i='prod_name';
        $columns[$i]['label'] = '名称';
        $columns[$i]['attribute'] = 'prod_name';
        $columns[$i]['options'] = ['style'=>'width:180px'];

        $columns[$i]['value']   = function($model) use ($sub_prod_spec_id)
        {
            return Html::a( $model->prod_name , ['classify', 'prod_spec_id'=> $sub_prod_spec_id ,   'parent_id'=> $model->id ], ['title' => "收录房源"]);

        };

//        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'prod_name', ['class'=>'form-control']);


    $i='contract_eff_date';
    //更新时间
    $columns[$i]['label'] = '生效时间';
    $columns[$i]['attribute'] = 'contract_eff_date';
    $columns[$i]['options'] = ['style'=>'width:80px'];
    $columns[$i]['content']   = function($model)
    {
        return  substr($model->contracts->eff_date,0,9) ;
        // date('Y-m-d H:i:s', $model->modify_date);
    };

    $i='contract_exp_date';
    //更新时间
    $columns[$i]['label'] = '失效时间';
    $columns[$i]['attribute'] = 'contract_exp_date';
    $columns[$i]['options'] = ['style'=>'width:80px'];
    $columns[$i]['content']   = function($model)
    {
        return  substr($model->contracts->exp_date,0,9) ;
        // date('Y-m-d H:i:s', $model->modify_date);
    };

    $i='early_warning';
    //更新时间
    $columns[$i]['label'] = '预警';
    $columns[$i]['attribute'] = 'early_warning';
    $columns[$i]['options'] = ['style'=>'width:80px'];
    $columns[$i]['content']   = function($model)
    {
        return  '暂无' ;
        // date('Y-m-d H:i:s', $model->modify_date);
    };


        if($prod_spec_id==10002){

            //房源状态
            $i='state';
            $columns[$i]['label'] = '状态';
            $columns[$i]['attribute'] = 'state';
            $columns[$i]['options'] = ['style'=>'width:180px'];
            //$columns[$i]['filter'] = Html::activeTextInput($searchModel, 'state', ['class'=>'form-control','style'=>'height:33px']);
            $columns[$i]['filter'] = Html::activeDropDownList($searchModel, 'state',['1002'=>'已租','1001'=>'空置']);
            $columns[$i]['value']   = function($model) use ($domainTypes)
            {
                return $domainTypes[$model->state];

            };
        }


        // 如果是楼盘需要取出子集
        if ($prod_spec_id==10001 || $prod_spec_id==10003 ||  $prod_spec_id==10004)
        {

            $i = 'sub_prod'; // 子集
            $columns[$i]['label'] = '收录房源';
            $columns[$i]['format'] = 'raw';

            $columns[$i]['options'] = ['style'=>'width:90px'];
            $columns[$i]['value']   = function($model) use ($sub_prod_spec_id)
            {
                return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=> $sub_prod_spec_id ,   'parent_id'=> $model->id ], ['title' => "收录房源"]);

            };

        }
        else if ($prod_spec_id==10002)
        {
            $i = 'parent_prod'; // 归属
            $columns[$i]['label'] = '归属楼盘';
            $columns[$i]['format'] = 'raw';
            //  $columns[$i]['attribute'] = 'parentProdInsts.prod_name';
            $columns[$i]['options'] = ['style'=>'width:180px'];
            $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'prod_name', ['class'=>'form-control','style'=>'height: 24px; padding:0px 0px;']);
            $columns[$i]['value']   = function($model) use($prodSpecName)
            {
                if (!$model->parentProdInsts)
                {
                    return Html::a('设置楼盘', ['update', 'prod_spec_id'=>10002,   'id'=> $model->id ,    'prod_spec_name' => $prodSpecName], ['title' => "归属楼盘"]);

                }
                //return Html::a($model->parentProdInsts->prod_name, ['view', 'id'=>$model->parentProdInsts->id,  'prod_spec_id'=>10001,  'prod_spec_name' =>'楼盘' ,'a'=>12345 ], ['title' => "归属楼盘"]);
                return Html::a($model->parentProdInsts->prod_name,
                    '#' ,
                    ['id' => 'a-view-link',
                        'class' => " a-view-link color-palette",
                        'data-toggle'=>'modal',
                        'data-target'=>'#a-view-modal',

                        'data-prod-id'=>$model->parentProdInsts->id,
                        'data-prod-spec-id'=> 10001,
                        'data-prod-spec-name' => '楼盘']);
            };
        }


        $i = 'total_area';
        $columns[$i]['label'] = '总面积';
        $columns[$i]['format'] = 'raw';

        $columns[$i]['options'] = ['style'=>'width:90px'];
        $columns[$i]['value']   = function($model) use ($prod_spec_id)
        {
            $unit = '㎡';
            $text = 0;
            if ($prod_spec_id==10002)
            {
                $model->totalArea = $model->buildAreasFirst->sumContent;

            }
            else{
                $model->totalArea = $model->buildAreas->sumContent;
            }

            if ($model->totalArea)
            {
                $text = $model->totalArea;

            }

            return $text . $unit;

        };

        $i = 'lended_area';
        $columns[$i]['label'] = '已租面积';
        $columns[$i]['format'] = 'raw';

        $columns[$i]['options'] = ['style'=>'width:90px'];
        $columns[$i]['value']   = function($model)
        {
            $unit = '㎡';
            $model->lendedArea = 0;
            $tmp = $model->selectLendedAreas($model->id, $model->prod_spec_id);
            if ($tmp['sumContent'])
            {
                $model->lendedArea = $tmp['sumContent'];
            }
            //Yii::trace("=====================              ============");
        //  $sumLendedContent = $model->lendedProds->lendedAreas->sumLendedContent;
            //Yii::trace($tmp );
            //return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=>10002,   'parent_id'=> $model->id ], ['title' => "收录房源"]);
            return  $model->lendedArea . $unit;
        };

        $i = 'vacancy_area';
        $columns[$i]['label'] = '空置面积';
        $columns[$i]['format'] = 'raw';

        $columns[$i]['options'] = ['style'=>'width:90px'];
        $columns[$i]['value']   = function($model)
        {
            $unit = '㎡';
            if(!$model->totalArea)
            {
                $model->totalArea = 0;
            }

            if(!$model->lendedArea)
            {
                $model->lendedArea = 0;
            }

            $model->vacancyArea = $model->totalArea - $model->lendedArea;
            //return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=>10002,   'parent_id'=> $model->id ], ['title' => "收录房源"]);
            return $model->vacancyArea . $unit;
        };

        $i = 'canvass_area';
        $columns[$i]['label'] = '可招商面积';
        $columns[$i]['format'] = 'raw';

        $columns[$i]['options'] = ['style'=>'width:90px'];
        $columns[$i]['value']   = function($model)
        {
            $unit = '㎡';
            $canvassAreas = $model->selectCanvassAreas($model->id, $model->prod_spec_id);
            $sumCanvass = 0;


            if ($canvassAreas['sumCanvass'])
            {
                $sumCanvass = $canvassAreas['sumCanvass'];
            }
            //return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=>10002,   'parent_id'=> $model->id ], ['title' => "收录房源"]);
            return $sumCanvass . $unit ;
        };

        $i = 'sum_lended_prices';
        $columns[$i]['label'] = '在租均价';
        $columns[$i]['format'] = 'raw';

        $columns[$i]['options'] = ['style'=>'width:90px'];
        $columns[$i]['value']   = function($model)
        {

            $unit = '元';
            $model->lendedPrice = 0;
            $tmp = $model->getLendedPrices($model->id, $model->prod_spec_id);
            if ($tmp['sumLendedPrices'])
            {
                $model->lendedPrice = $tmp['sumLendedPrices'];
            }
            //Yii::trace("=====================              ============");
            //  $sumLendedContent = $model->lendedProds->lendedAreas->sumLendedContent;
            //Yii::trace($tmp );
            //return Html::a(count($model->subProdInsts), ['classify', 'prod_spec_id'=>10002,   'parent_id'=> $model->id ], ['title' => "收录房源"]);
            return  $model->lendedPrice . $unit;
        };



        $i = 'use_cust_count';
        $columns[$i]['label'] = '入驻企业';
        $columns[$i]['format'] = 'raw';
        $columns[$i]['options'] = ['style'=>'width:90px'];
        $columns[$i]['value']   = function($model)
        {
            $unit = '家';
            $tmp = $model->getUseCustCount($model->id, $model->prod_spec_id);
//            Yii::trace("=====================              ============");
//            Yii::trace($tmp);

            if (!$tmp)
            {
                $tmp=0;
            }

            return $tmp . $unit;
        };

        foreach ($attrSpecs as $attrSpec)
        {
            $prodAttr = $attrSpec->attrSpecs;

            if ( $attrSpec->grid_show_flag!=1)
            {
                continue;
            }

            $i = $prodAttr->attr_field_name;
            $columns[$i]['label'] = $prodAttr->attr_spec_name;
            $prodAttrCodes[$i] = $prodAttr->attr_spec_id;
            $a = $prodAttr->attr_spec_id;
            $attr_spec_type_id = $prodAttr->attr_spec_type_id;
            if($columns[$i]['label']=='门牌号码'){//??出不来   4
                //$columns[$i]['filter'] = Html::activeTextInput($searchModel, 'mp', ['class'=>'form-control']);
            }elseif($columns[$i]['label']=='楼层'){//14

                $columns[$i]['filter'] = Html::activeDropDownList($searchModel, 'content',['1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5']);
            }


            $columns[$i]['value']   = function($model, $key, $index, $column)use ($a, $attr_spec_type_id, $domains)
            {
                if ($model->prodAttrInsts) {
                    foreach ($model->prodAttrInsts as $attrInst) {
                        if ($a == $attrInst->attr_spec_id) {
                            if ($attr_spec_type_id == 3) {
                                return $domains['attr_spec'][$a] [$attrInst->content];
                            } else if ($attr_spec_type_id == 4) {
                                return $attrInst->content . '-' . $attrInst->content2;
                            }

                            return $attrInst->content;

                        }
                    }
                }
            };


            $width = '120px';
            if($prodAttr->attr_spec_width)
            {
                $width = 'width:' . $prodAttr->attr_spec_width . 'px';
            }

            $columns[$i]['options'] = ['style'=>$width];


        }




        $columns2 = Functions::getFormatColumns($customs, $columns);
        $ii = count($columns2);

        //操作选项
        $columns2[$ii]['options'] = ['style'=>'width:90px'];

        $columns2[$ii]['content']   = function($model)use ($prodSpecName, $now_url,$commonality)
        {
            $btns = [];

            $btnBegin = Html::a( '查看',  '#' ,
                ['id' => 'activity-view-link',
                    'class' => "btn btn-xs btn-warning activity-view-link",
                    'data-toggle'=>'modal',
                    'data-target'=>'#view-modal',
                    'label'=>'查看子产品', 'title' => '查看子产品',
                    'data-prod-id'=>$model->id,
                    'data-prod-spec-id'=> $model->prod_spec_id,
                    'data-prod-spec-name' => $prodSpecName]);

            $btns[]   = Html::a( '查看',  '#' ,
                ['id' => 'activity-view-link',
                    'class' => "activity-view-link",
                    'data-toggle'=>'modal',
                    'data-target'=>'#view-modal',
                    'label'=>'查看子产品',
                    'title' => '查看子产品',
                    'data-prod-id'=>$model->id,
                    'data-prod-spec-id'=> $model->prod_spec_id,
                    'data-prod-spec-name' => $prodSpecName]);


            if( $commonality == -1){//回调函数 参数要传进来，不然用不了
                $btns[]   = Html::a( '抢单', ['commonality', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,   'last_url'  => $now_url, 'prod_spec_name' => $prodSpecName ], [ 'label'=>'抢单', 'title' => '抢单']);

            }
            if($commonality != -1  && $commonality != null){
                $btns[]   = Html::a( '变更负责人',   '#',
                    ['id' => 'activity-modify-uesr-link',
                        'class' => "activity-modify-uesr-link",
                        'data-toggle'=>'modal',
                        'data-target'=>'#modify-uesr-modal',
                        'label'=>'变更负责人', 'title' => '变更负责人',
                        'data-order-id'=>$model->id,
                        'data-last_url' =>$now_url]);
            }


            $btns[]   = Html::a( '编辑',  '#' ,
                ['id' => 'activity-update-prod-link',
                    'class' => "activity-update-prod-link",
                    'data-toggle'=>'modal',
                    'data-target'=>'#update-prod-modal',
                    'label'=>'编辑产品',
                    'title' => '编辑产品',
                    'data-prod_id'=>$model->id,
                    'data-prod_spec_id'=> $model->prod_spec_id,
                    'data-last_url'  => $now_url,
                    'data-prod_spec_name' => $prodSpecName]);
            //$btns[]   = Html::a( '编辑', ['update', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,   'last_url'  => $now_url, 'prod_spec_name' => $prodSpecName ], [ 'label'=>'编辑', 'title' => '编辑']);
            $btns[]   = Html::a( '删除', ['delete', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id, 'now_url'=> $now_url ], [ 'label'=>'删除', 'title' => '删除']);


            return Functions::getActionDropdownContext($btnBegin, $btns);
            // return  substr($model->create_date,0,16) ;
            // date('Y-m-d H:i:s', $model->modify_date);
        };

//        $i++;
//        //操作选项
//        $columns[$i]['class']   = 'app\common\grid\CustActionColumn';
//         $columns[$i]['options'] = ['style'=>'width:180px'];
//       // $columns[$i]['headerOptions'] = ['width' => '20%', 'class' => 'activity-view-link'];
//        $columns[$i]['buttons'] =  [
//            'view' => function ($url, $model, $key) use ($prodSpecName) {
//                return Html::a('查看', // ['view', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,    'prod_spec_name' => $prodSpecName]
//                   '#' ,
//                    ['id' => 'activity-view-link',
//                        'class' => "btn btn-xs btn-success activity-view-link",
//                        'data-toggle'=>'modal',
//                        'data-target'=>'#view-modal',
//                        'label'=>'查看子产品', 'title' => '查看子产品',
//                        'data-prod-id'=>$model->id,
//                        'data-prod-spec-id'=> $model->prod_spec_id,
//                        'data-prod-spec-name' => $prodSpecName]);
//            },
//
//
//
//            'update' => function ($url, $model, $key) use ($prodSpecName) {
//                return Html::a(Html::tag('span', '编辑', ['class' => "glyphicon fa fa-edit"]),
//                    ['update', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id,    'prod_spec_name' => $prodSpecName ],
//                    ['class' => "btn btn-xs btn-success",'label'=>'查看子产品', 'title' => '查看子产品']);
//            },
//
//            'delete' => function ($url, $model, $key)  {
//                return Html::a(Html::tag('span', '删除', ['class' => "glyphicon fa fa-trash"]), ['delete', 'id'=>$model->id,   'prod_spec_id'=> $model->prod_spec_id ], ['class' => "btn btn-xs btn-danger",'label'=>'删除产品', 'title' => '删除子产品']);
//            },
//
//
//            'create' => function ($url, $model, $key) use ($prodSpecName) {
//                return Html::a(Html::tag('span', '订单生成', ['class' => "glyphicon fa fa-edit"]), ['/prod/order/create', 'prod_id'=>$model->id,       'prod_spec_name' => $prodSpecName ], ['class' => "btn btn-xs btn-success",'label'=>'生成一张订单', 'title' => '查看子产品']);
//            },
//
//        ];




    //Pjax::begin();

        echo GridView::widget([
        'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
        'layout' => "{items}{summary}{pager}",
        'options'=>['id'=>'grid'],
        'columns' => $columns2

        ]);





    ?>



